home *** CD-ROM | disk | FTP | other *** search
- /*
- File: RectShpe.h
-
- Contains: RealShape class, private to ODShape.
-
- Owned by: Jens Alfke
-
- Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <7> 9/13/95 DM 1277216 GM:API return no ODPoints nor
- ODPolygons
- <6> 8/16/95 NP 1274946: ErrorDef.idl problems. Add include
- file.
- <5> 5/25/95 jpa Use new GX headers [1241078, 1253324]
- <4> 12/5/94 jpa gxSolidFill -> gxWindingFill. [1191192]
- <3> 8/8/94 jpa Added Outset method [1178690]
- <2> 8/2/94 jpa Use AsPolygonShape, not Promote, in
- Transform method.
- <1> 6/15/94 jpa first checked in
-
- In Progress:
- */
-
-
- #ifndef _ALTPOINT_
- #include "AltPoint.h" /* Use C++ savvy XMPPoint and XMPRect*/
- #endif
-
- #ifndef _ALTPOLY_
- #include "AltPoly.h"
- #endif
-
- #ifndef _RECTSHPE_
- #include "RectShpe.h"
- #endif
-
- #ifndef _PolySHPE_
- #include "PolyShpe.h"
- #endif
-
- #ifndef SOM_ODTransform_xh
- #include "Trnsform.xh"
- #endif
-
- #ifndef _EXCEPT_
- #include "Except.h"
- #endif
-
- #ifndef _UTILERRS_
- #include "UtilErrs.h"
- #endif
-
- #ifndef __GXGRAPHICS__
- #include <GXGraphics.h>
- #endif
-
-
- #pragma segment ODShape
-
-
- RectShape::RectShape( ODGeometryMode mode, const ODRect &rect )
- :RealShape(mode),
- fRect(rect)
- {
- #if ODDebug
- fType = 0;
- #endif
- }
-
-
- void
- RectShape::GetBoundingBox( ODRect *bounds )
- {
- *bounds = fRect;
- }
-
-
- RealShape*
- RectShape::SetRectangle( const ODRect *r )
- {
- fRect = *r;
- this->Purge(0);
- return this;
- }
-
-
- void
- RectShape::CopyPolygon( ODPolygon &poly )
- {
- poly.SetRect(fRect);
- }
-
-
- void
- RectShape::InitQDRegion( )
- {
- Rect r;
- fRect.AsQDRect(r);
- RectRgn(fQDRegion,&r);
- }
-
-
- gxShape
- RectShape::CopyGXShape( )
- {
- ASSERT(gGX>0,kODErrAssertionFailed);
- gxShape r = GXNewRectangle( (gxRectangle*) &fRect );
- GXSetShapeFill(r,gxWindingFill);
- ThrowIfGXError();
- return r;
- }
-
-
- ODBoolean
- RectShape::IsSameAs( RealShape *shape )
- {
- if( !shape->IsRectangular() )
- return kODFalse;
- ODRect r;
- shape->GetBoundingBox(&r);
- return r == fRect;
- }
-
-
- ODBoolean
- RectShape::IsEmpty( )
- {
- return fRect.IsEmpty();
- }
-
-
- ODBoolean
- RectShape::ContainsPoint( ODPoint point )
- {
- return fRect.Contains(point);
- }
-
-
- ODBoolean
- RectShape::IsRectangular( )
- {
- return kODTrue; // Duhhhhhh...
- }
-
-
- RealShape*
- RectShape::Clear( )
- {
- fRect.Clear();
- this->Purge(0);
- return this;
- }
-
-
- RealShape*
- RectShape::Copy( )
- {
- RectShape *r = new RectShape(fMode,fRect);
- return r->SetRectangle(&fRect);
- }
-
-
- RealShape*
- RectShape::Transform( Environment *ev, ODTransform *xform )
- {
- if( fRect.IsEmpty() )
- ; // I'm empty: do nothing
-
- else if( xform->GetType(ev) <= kODScaleTranslateXform ) {
- ODPoint topLeft = fRect.TopLeft();
- ODPoint botRight = fRect.BotRight();
-
- xform->TransformPoint(ev,&topLeft);
- xform->TransformPoint(ev,&botRight);
-
- fRect = ODRect(topLeft, botRight);
- this->Purge(0);
-
- } else {
- RealShape* s = this->AsPolygonShape(); // General case: Promote myself
- TRY{
- s= s->Transform(ev,xform); // ...and ask the promoted shape to do the job.
- }CATCH_ALL{
- delete s;
- RERAISE;
- }ENDTRY
- delete this;
- return s;
- }
-
- return this;
- }
-
-
- RealShape*
- RectShape::Outset( ODCoordinate distance )
- {
- if( ! fRect.IsEmpty() ) {
- fRect.Inset(-distance,-distance);
- this->Purge(0);
- }
- return this;
- }
-
-
- RealShape*
- RectShape::Subtract( RealShape *shape )
- {
- if( shape->IsEmpty() || this->IsEmpty() ) // I (heart) no-ops
- return this;
-
- else if( shape==this ) // S - S = 0
- return this->Clear();
-
- else {
- ODRect itsBounds;
- shape->GetBoundingBox(&itsBounds);
- if( !fRect.Intersects(itsBounds) )
- return this;
- else
- return this->Promote(kShapeDifference,shape);
- }
- }
-
-
- RealShape*
- RectShape::Intersect( RealShape *shape )
- {
- ODRect r;
-
- if( shape==this )
- return this;
-
- shape->GetBoundingBox(&r);
-
- if( shape->IsRectangular() ) {
- fRect &= r; // Intersection of two rects is a rect
- this->Purge(0);
-
- } else if( this->IsEmpty() || shape->IsEmpty() || !fRect.Intersects(r) ) {
- fRect.Clear(); // One of us is empty, or we don't intersect
- this->Purge(0); // so make myself empty
-
- } else if( fRect.Contains(r) ) // I contain the shape, so make myself a copy of it
- return this->ReplaceWith(shape);
-
- else
- return this->Promote(kShapeIntersection,shape);
-
- return this;
- }
-
-
- RealShape*
- RectShape::Union( RealShape *shape )
- {
- if( shape==this || shape->IsEmpty() ) // Shape is me or is empty: no-op
- return this;
-
- if( this->IsEmpty() ) // I am empty: change to a copy of the other shape
- return this->ReplaceWith(shape);
-
- ODRect bounds;
- shape->GetBoundingBox(&bounds);
- if( fRect.Contains(bounds) )
- ; // Shape is contained in me: no-op
-
- else if( shape->IsRectangular() && bounds.Contains(fRect) ) {
- fRect = bounds;
- this->Purge(0); // I am contained in another rectangle:
- // just grow my bounds to its
- } else
- return this->Promote(kShapeUnion,shape);
-
- return this;
- }
-